package cn.edu.hitsz.conf;

import java.io.File;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

import cn.edu.hitsz.core.Connection;
import cn.edu.hitsz.core.DatabaseConnection;
import cn.edu.hitsz.core.FileConnection;

/**
 * THIS IS A SPIDER CONFIGURATION CLASSS TO SET ALL THE PARAMETERS FOR CLAWLING
 * AND ALL THE PARAMETER ARE SET IN PROPERTIES FILE
 * 
 * WITH THE DEVELOPMENT, SOME PARAMETERS ARE DEPRECATED AND SOME ARE PACKAGE
 * INTO ON OR MORE PACKAGE IN CLASS MODE
 * 
 * HOST,PORT,DBUSER,DBPASSWORD -----> DATABASECONIF SAVEFILEROOTDIRECTORY,
 * MAXFIELSPERDIR -----> FILECONFIG
 * 
 * @author mingchen
 * @date 2015年8月8日
 */
public class SpiderConfig {
	// private final static Category LogClass =
	// Category.getInstance(SpiderConfig.class);
	private final static Logger LogClass = Logger.getLogger(SpiderConfig.class);

	/**
	 * ALL TEH PARAMETERS NECESSARY
	 */
	private File SaveFileRootDirectory;
	private final int MaxFilesPerDir;
	private String DBDriver;
	private String Host;
	private int Port;
	private String DBUser;
	private String DBPassword;
	private Set<String> PageExtensions;
	private Set<String> ImageExtensions;
	private Set<String> StartURLs;
	private boolean DepthFirst;
	private int MaxDepth;
	private int MaxIntervalPerRequest;
	private String UserAgent;
	private int SpiderThreads;
	public static Logger getLogclass() {
		return LogClass;
	}

	public Connection getDataconn() {
		return dataconn;
	}

	private String AuthUserName;
	private String AuthUserPwd;

	private Connection dataconn;

	/**
	 * 
	 * Constructor function of Class
	 * 
	 * @param pps
	 *            Properties file
	 */
	public SpiderConfig(Properties pps) {
		LogClass.debug("In SpiderConfig Class Constructor method");
		if (pps.containsKey("filedir")) {// storage in local file system
			SaveFileRootDirectory = new File(pps.getProperty("filedir", "."));
			if (!SaveFileRootDirectory.exists()) {
				if (!SaveFileRootDirectory.mkdirs()) {
					LogClass.error("Cannot create directory" + SaveFileRootDirectory.getName());
					LogClass.info("Default instead");
					SaveFileRootDirectory = new File(".");
				}
			} else if (!SaveFileRootDirectory.isDirectory()) {
				LogClass.error("The filedir"+SaveFileRootDirectory.getName()+" is not a directory!!");
				LogClass.info("Default instead");
				SaveFileRootDirectory = new File(".");
				dataconn = new FileConnection(FileConfig.getFileConfig());
			}
		} else if (pps.containsKey("")) { // storage in local database
			// DBDriver = pps.getProperty("dbdriver");
			// Host = pps.getProperty("host");
			// Port = Integer.parseInt(pps.getProperty("port"));
			// DBUser = pps.getProperty("dbuser");
			// DBPassword = pps.getProperty("dbpassord");
			dataconn = new DatabaseConnection(DatabaseConfig.getDBConfig());
		} else { // storage in file system as default
			LogClass.error("Now data storage set !!");
			LogClass.info("Local file storage instead");
			SaveFileRootDirectory = new File(".");
		}
		// set the crawl direction [depth or not]
		DepthFirst = Boolean.getBoolean(pps.getProperty("depthfirst", "false"));

		// set the max depth default as 3
		MaxDepth = Integer.parseInt(pps.getProperty("maxdepth", "3"));

		// set the extension of the page html as default
		PageExtensions = parseSet(pps.getProperty("pageext", "html"));

		// set image extension of to retrieve during developing
		ImageExtensions = parseSet(pps.getProperty("imageext", "jpeg,gif"));

		UserAgent = pps.getProperty("useragent", "fire fox 3.5l");

		AuthUserName = pps.getProperty("userName", "guest");
		AuthUserPwd = pps.getProperty("userpwd", "anonymous");

		SpiderThreads = Integer.parseInt(pps.getProperty("threads", "4"));
		// set the max interval default as 5
		MaxIntervalPerRequest  = Integer.parseInt(pps.getProperty("maxintervalperreq", "5"));

		MaxFilesPerDir = Integer.parseInt(pps.getProperty("maxfilesperdir", "100"));


	}

	/**
	 * @return the dBDriver
	 */
	public String getDBDriver() {
		return DBDriver;
	}

	/**
	 * @param dBDriver
	 *            the dBDriver to set
	 */
	public void setDBDriver(String dBDriver) {
		DBDriver = dBDriver;
	}

	/**
	 * @return the dBUser
	 */
	public String getDBUser() {
		return DBUser;
	}

	/**
	 * @param dBUser
	 *            the dBUser to set
	 */
	public void setDBUser(String dBUser) {
		DBUser = dBUser;
	}

	/**
	 * @return the dBPassword
	 */
	public String getDBPassword() {
		return DBPassword;
	}

	/**
	 * @param dBPassword
	 *            the dBPassword to set
	 */
	public void setDBPassword(String dBPassword) {
		DBPassword = dBPassword;
	}

	/**
	 * @return the maxFilesPerDir
	 */
	public int getMaxFilesPerDir() {
		return MaxFilesPerDir;
	}

	/**
	 * Parse the comma seperated string to set
	 * 
	 * @param str
	 * @return Set 2015年8月8日
	 */
	private Set parseSet(String str) {
		LogClass.debug("parseSet(" + str + ")");
		HashSet result = new HashSet();
		StringTokenizer sTok = new StringTokenizer(str, ",");
		while (sTok.hasMoreTokens()) {
			String tok = sTok.nextToken().trim();
			result.add(tok);
		}
		return result;
	}
	/**
	 * @return the dbDriver
	 */
	public String getDbDriver() {
		return DBDriver;
	}

	/**
	 * @param dbDriver
	 *            the dbDriver to set
	 */
	public void setDbDriver(String dbDriver) {
		DBDriver = dbDriver;
	}

	/**
	 * @return the host
	 */
	public String getHost() {
		return Host;
	}

	/**
	 * @param host
	 *            the host to set
	 */
	public void setHost(String host) {
		Host = host;
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return Port;
	}

	/**
	 * @param port
	 *            the port to set
	 */
	public void setPort(int port) {
		Port = port;
	}

	/**
	 * @return the saveFileRootDirectory
	 */
	public File getSaveFileRootDirectory() {
		return SaveFileRootDirectory;
	}

	/**
	 * @param saveFileRootDirectory
	 *            the saveFileRootDirectory to set
	 */
	public void setSaveFileRootDirectory(File saveFileRootDirectory) {
		SaveFileRootDirectory = saveFileRootDirectory;
	}

	/**
	 * @return the pageExtensions
	 */
	public Set<String> getPageExtensions() {
		return PageExtensions;
	}

	/**
	 * @param pageExtensions
	 *            the pageExtensions to set
	 */
	public void setPageExtensions(Set<String> pageExtensions) {
		PageExtensions = pageExtensions;
	}

	/**
	 * @return the imageExtensions
	 */
	public Set<String> getImageExtensions() {
		return ImageExtensions;
	}

	/**
	 * @param imageExtensions
	 *            the imageExtensions to set
	 */
	public void setImageExtensions(Set<String> imageExtensions) {
		ImageExtensions = imageExtensions;
	}

	/**
	 * @return the startURLs
	 */
	public Set<String> getStartURLs() {
		return StartURLs;
	}

	/**
	 * @param startURLs
	 *            the startURLs to set
	 */
	public void setStartURLs(Set<String> startURLs) {
		StartURLs = startURLs;
	}

	/**
	 * @return the depthFirst
	 */
	public boolean isDepthFirst() {
		return DepthFirst;
	}

	/**
	 * @param depthFirst
	 *            the depthFirst to set
	 */
	public void setDepthFirst(boolean depthFirst) {
		DepthFirst = depthFirst;
	}

	/**
	 * @return the maxDepth
	 */
	public int getMaxDepth() {
		return MaxDepth;
	}

	/**
	 * @param maxDepth
	 *            the maxDepth to set
	 */
	public void setMaxDepth(int maxDepth) {
		MaxDepth = maxDepth;
	}

	/**
	 * @return the maxIntervalPerRequest
	 */
	public int getMaxIntervalPerRequest() {
		return MaxIntervalPerRequest;
	}

	/**
	 * @param maxIntervalPerRequest
	 *            the maxIntervalPerRequest to set
	 */
	public void setMaxIntervalPerRequest(int maxIntervalPerRequest) {
		MaxIntervalPerRequest = maxIntervalPerRequest;
	}

	/**
	 * @return the userAgent
	 */
	public String getUserAgent() {
		return UserAgent;
	}

	/**
	 * @param userAgent
	 *            the userAgent to set
	 */
	public void setUserAgent(String userAgent) {
		UserAgent = userAgent;
	}

	/**
	 * @return the spiderThreads
	 */
	public int getSpiderThreads() {
		return SpiderThreads;
	}

	/**
	 * @param spiderThreads
	 *            the spiderThreads to set
	 */
	public void setSpiderThreads(int spiderThreads) {
		SpiderThreads = spiderThreads;
	}

	/**
	 * @return the authUserName
	 */
	public String getAuthUserName() {
		return AuthUserName;
	}

	/**
	 * @param authUserName
	 *            the authUserName to set
	 */
	public void setAuthUserName(String authUserName) {
		AuthUserName = authUserName;
	}

	/**
	 * @return the authUserPwd
	 */
	public String getAuthUserPwd() {
		return AuthUserPwd;
	}

	/**
	 * @param authUserPwd
	 *            the authUserPwd to set
	 */
	public void setAuthUserPwd(String authUserPwd) {
		AuthUserPwd = authUserPwd;
	}

	/**
	 * Override the toString method
	 */
	@Override
	public String toString() {
		String str = "StartURLs: "+StartURLs.toString() +"\n"+
				"DepthFirst:"+DepthFirst+"\n"+
				"MaxDepth:"+MaxDepth +"\n"+
				"MaxIntervalPerRequest:" + MaxIntervalPerRequest+"\n"+
				"PageExtensions:"+PageExtensions.toString();
		return str;
	}

}
